/*
 * Copyright 2011 Guilherme José de Carvalho Gois
  *
 * Licensed under the Apache License, Version 2.0 (the “License”);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an “AS IS” BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */

/*
 * FrmPessoas.java
 *
 * Created on 12/11/2011, 10:52:12
 */
package view;

import java.awt.Color;
import javax.swing.JOptionPane;

/**
 *
 * @author guijocargo
 */
 
public class FrmPessoas extends javax.swing.JFrame {

    /** Creates new form FrmPessoas */
    public FrmPessoas() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        pnlAtributos = new javax.swing.JPanel();
        chkNome = new javax.swing.JCheckBox();
        chkSobrenome = new javax.swing.JCheckBox();
        chkDtNasc = new javax.swing.JCheckBox();
        chkTelefone = new javax.swing.JCheckBox();
        chkEmail = new javax.swing.JCheckBox();
        txtNomeTabela = new javax.swing.JTextField();
        pnlCodigoSql = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtCodigoSql = new javax.swing.JTextArea();
        btnGerarCodigoSql = new javax.swing.JButton();
        lblQntdInsert = new javax.swing.JLabel();
        txtQntdInsert = new javax.swing.JTextField();
        pnlAcoes = new javax.swing.JPanel();
        btnLimparCodigo = new javax.swing.JButton();
        btnExportarSql = new javax.swing.JButton();
        btnResetar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Povoador SQL - Pessoas");

        pnlAtributos.setBorder(javax.swing.BorderFactory.createTitledBorder("Atributos"));

        chkNome.setText("Nome");

        chkSobrenome.setText("Sobrenome");
        chkSobrenome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkSobrenomeActionPerformed(evt);
            }
        });

        chkDtNasc.setText("Dt. de Nasc.");
        chkDtNasc.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                chkDtNascActionPerformed(evt);
            }
        });

        chkTelefone.setText("Telefone");

        chkEmail.setText("E-mail");

        javax.swing.GroupLayout pnlAtributosLayout = new javax.swing.GroupLayout(pnlAtributos);
        pnlAtributos.setLayout(pnlAtributosLayout);
        pnlAtributosLayout.setHorizontalGroup(
            pnlAtributosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAtributosLayout.createSequentialGroup()
                .addGroup(pnlAtributosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(chkNome)
                    .addComponent(chkSobrenome)
                    .addComponent(chkDtNasc)
                    .addComponent(chkTelefone)
                    .addComponent(chkEmail))
                .addContainerGap(52, Short.MAX_VALUE))
        );
        pnlAtributosLayout.setVerticalGroup(
            pnlAtributosLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAtributosLayout.createSequentialGroup()
                .addComponent(chkNome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkSobrenome)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkDtNasc)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkTelefone)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(chkEmail)
                .addContainerGap(62, Short.MAX_VALUE))
        );

        txtNomeTabela.setForeground(java.awt.Color.gray);
        txtNomeTabela.setText("Nome da Tabela");
        txtNomeTabela.setToolTipText("Insira aqui o nome de sua tabela.");
        txtNomeTabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtNomeTabelaMouseClicked(evt);
            }
        });
        txtNomeTabela.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNomeTabelaActionPerformed(evt);
            }
        });
        txtNomeTabela.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtNomeTabelaKeyReleased(evt);
            }
        });

        pnlCodigoSql.setBorder(javax.swing.BorderFactory.createTitledBorder("Código SQL"));

        txtCodigoSql.setColumns(20);
        txtCodigoSql.setRows(5);
        jScrollPane1.setViewportView(txtCodigoSql);

        javax.swing.GroupLayout pnlCodigoSqlLayout = new javax.swing.GroupLayout(pnlCodigoSql);
        pnlCodigoSql.setLayout(pnlCodigoSqlLayout);
        pnlCodigoSqlLayout.setHorizontalGroup(
            pnlCodigoSqlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 246, Short.MAX_VALUE)
        );
        pnlCodigoSqlLayout.setVerticalGroup(
            pnlCodigoSqlLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
        );

        btnGerarCodigoSql.setText("Gerar Código SQL");
        btnGerarCodigoSql.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGerarCodigoSqlActionPerformed(evt);
            }
        });

        lblQntdInsert.setText("Qntd. de INSERT");

        txtQntdInsert.setForeground(java.awt.Color.gray);
        txtQntdInsert.setHorizontalAlignment(javax.swing.JTextField.RIGHT);
        txtQntdInsert.setText("000");
        txtQntdInsert.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                txtQntdInsertMouseClicked(evt);
            }
        });
        txtQntdInsert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtQntdInsertActionPerformed(evt);
            }
        });
        txtQntdInsert.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtQntdInsertKeyReleased(evt);
            }
        });

        pnlAcoes.setBorder(javax.swing.BorderFactory.createTitledBorder("Ações"));

        btnLimparCodigo.setText("Limpar Código");
        btnLimparCodigo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparCodigoActionPerformed(evt);
            }
        });

        btnExportarSql.setText("Exportar SQL");

        btnResetar.setText("Resetar");
        btnResetar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlAcoesLayout = new javax.swing.GroupLayout(pnlAcoes);
        pnlAcoes.setLayout(pnlAcoesLayout);
        pnlAcoesLayout.setHorizontalGroup(
            pnlAcoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(btnExportarSql, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
            .addComponent(btnLimparCodigo, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
            .addComponent(btnResetar, javax.swing.GroupLayout.DEFAULT_SIZE, 137, Short.MAX_VALUE)
        );
        pnlAcoesLayout.setVerticalGroup(
            pnlAcoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlAcoesLayout.createSequentialGroup()
                .addComponent(btnExportarSql)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnLimparCodigo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnResetar))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(pnlAcoes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(pnlAtributos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlCodigoSql, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtNomeTabela, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(lblQntdInsert)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtQntdInsert, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnGerarCodigoSql)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNomeTabela, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblQntdInsert)
                    .addComponent(txtQntdInsert, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnGerarCodigoSql))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlAtributos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(pnlAcoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(pnlCodigoSql, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );

        pack();
    }// </editor-fold>

    private void chkDtNascActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void chkSobrenomeActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
    }

    private void btnResetarActionPerformed(java.awt.event.ActionEvent evt) {
        
        boolean f = false;
        
        txtNomeTabela.setForeground(Color.GRAY);
        txtNomeTabela.setText("Nome da Tabela");
        txtQntdInsert.setForeground(Color.GRAY);
        txtQntdInsert.setText("000");
        
        chkDtNasc.setSelected(f);
        chkEmail.setSelected(f);
        chkNome.setSelected(f);
        chkSobrenome.setSelected(f);
        chkTelefone.setSelected(f);
        
        txtCodigoSql.setText(null);
    }

    private void btnLimparCodigoActionPerformed(java.awt.event.ActionEvent evt) {
        txtCodigoSql.setText(null);
    }

    private void txtNomeTabelaActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void txtQntdInsertActionPerformed(java.awt.event.ActionEvent evt) {
    }

    private void txtNomeTabelaKeyReleased(java.awt.event.KeyEvent evt) {
        if (txtNomeTabela.getForeground() == Color.GRAY) {
            txtNomeTabela.setText(String.valueOf(evt.getKeyChar()));
            txtNomeTabela.setForeground(Color.BLACK);
        }
    }

    private void txtQntdInsertKeyReleased(java.awt.event.KeyEvent evt) {
        int len = txtQntdInsert.getText().length() - 1;
        
        try {
            int n = Integer.parseInt(txtQntdInsert.getText());
            if (txtQntdInsert.getForeground() == Color.GRAY) {
                txtQntdInsert.setText(String.valueOf(evt.getKeyChar()));
                txtQntdInsert.setForeground(Color.BLACK);
            }
        } catch (Exception ex) {
            txtQntdInsert.setText(txtQntdInsert.getText().substring(0, len));
        }
    }

    private void txtNomeTabelaMouseClicked(java.awt.event.MouseEvent evt) {
        if (txtNomeTabela.getForeground() == Color.GRAY) {
            txtNomeTabela.setText(null);
            txtNomeTabela.setForeground(Color.BLACK);
        }
    }

    private void txtQntdInsertMouseClicked(java.awt.event.MouseEvent evt) {
        if (txtQntdInsert.getForeground() == Color.GRAY) {
                txtQntdInsert.setText(null);
                txtQntdInsert.setForeground(Color.BLACK);
            }
    }

    private void btnGerarCodigoSqlActionPerformed(java.awt.event.ActionEvent evt) {
        
        //TODO verificar por que o primeiro campo esta vindo com null
        
        String nomeTabela = txtNomeTabela.getText();
        String campos = null;
        int numInsert = Integer.parseInt(txtQntdInsert.getText());
        boolean b = false;
        
        if(chkNome.isSelected()) campos += JOptionPane.showInputDialog(null, "Nome do cam"
                + "po " + chkNome.getText()) + ", ";
        if(chkSobrenome.isSelected()) campos += JOptionPane.showInputDialog(null, "Nome do cam"
                + "po " + chkSobrenome.getText()) + ", ";
        if(chkDtNasc.isSelected()) campos += JOptionPane.showInputDialog(null, "Nome do cam"
                + "po " + chkDtNasc.getText()) + ", ";
        if(chkTelefone.isSelected()) campos += JOptionPane.showInputDialog(null, "Nome do cam"
                + "po " + chkTelefone.getText()) + ", ";
        if(chkEmail.isSelected()) campos += JOptionPane.showInputDialog(null, "Nome do cam"
                + "po " + chkEmail.getText()) + ", ";
        
        if(campos.endsWith(", ")) campos = campos.substring(0, campos.length() 
                - 2);
        
        for(int i = 0; i < numInsert; i++){
            if(b == false){
                txtCodigoSql.setText("INSERT INTO " + nomeTabela + "(" + campos
                        + ")\n\tVALUES();");
                b = true;
            } else {
                txtCodigoSql.setText(txtCodigoSql.getText() + "\nINSERT INTO "
                    + nomeTabela + "(" + campos + ")\n\tVALUES();");
            }
        }
        
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new FrmPessoas().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btnExportarSql;
    private javax.swing.JButton btnGerarCodigoSql;
    private javax.swing.JButton btnLimparCodigo;
    private javax.swing.JButton btnResetar;
    private javax.swing.JCheckBox chkDtNasc;
    private javax.swing.JCheckBox chkEmail;
    private javax.swing.JCheckBox chkNome;
    private javax.swing.JCheckBox chkSobrenome;
    private javax.swing.JCheckBox chkTelefone;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblQntdInsert;
    private javax.swing.JPanel pnlAcoes;
    private javax.swing.JPanel pnlAtributos;
    private javax.swing.JPanel pnlCodigoSql;
    private javax.swing.JTextArea txtCodigoSql;
    private javax.swing.JTextField txtNomeTabela;
    private javax.swing.JTextField txtQntdInsert;
    // End of variables declaration
}
